package spring.angularjs.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.fabric.xmlrpc.base.Array;

import spring.angularjs.domain.Account;
import spring.angularjs.domain.Customer;
import spring.angularjs.domain.json.AccountJson;
import spring.angularjs.domain.json.CustomerJson;
import spring.angularjs.services.Services;

@Controller
@RequestMapping(value="/customer")
public class CustomerController {
	
	@Autowired
	private Services services;

	@RequestMapping(value="/start")
	public String goPartialCustomer(){
		return "customer";
	}
	
	@RequestMapping(value="/getList", method=RequestMethod.GET, headers="Accept=application/json")
	@ResponseBody
	public List<CustomerJson> getListCustomer(){
		List<Customer> listCustomer = services.getListCustomer();
		List<CustomerJson> list = new ArrayList<CustomerJson>();
		for (int i = 0; i < listCustomer.size(); i++) {
			Customer c = listCustomer.get(i);
			CustomerJson cj = new CustomerJson(c.getIdcustomer(), c.getName(), c.getBirthday(), c.getIdcardnumber());
			list.add(cj);
		}
		return list;
	}
	
	@RequestMapping(value="/listAccount/{id}", method=RequestMethod.POST, headers="Accept=application/json")
	@ResponseBody
	public List<AccountJson> getListAccountOfCustomer(@PathVariable("id") String id){
		List<Account> listAccount = services.getListAccountOfCustomer(id);
		List<AccountJson> list = new ArrayList<AccountJson>();
		for (int i = 0; i < listAccount.size(); i++) {
			Account a = listAccount.get(i);
			AccountJson b = new AccountJson(a.getIdaccount(), a
					.getAccountType().getNametype(), a.getStatusaccount()
					.getName(), a.getCustomer().getIdcustomer(),
					a.getNameaccount(), a.getBalance());
			list.add(b);
		}
		return list;
	}
}
